# merge lad bootstraps
library(haven)

n_bootstrap=1000

filepathlad="H:/Zheng_10223/Joint/LAD/bootstrap/"

ladout=lapply(1982:2019, function(yy){
  
  print(yy)
  bootstrapout=lapply(1:n_bootstrap, function(ii){
    
    filename_ind=paste("ind/pctind_sample",ii,"_year",yy,".dta", sep="")
    
    filename_parents=paste(filepathlad,"parents/pctparent_sample",ii,"_year",yy,".dta", sep="")
    
    dfind=read_dta(filename_ind)
    dfparents=read_dta(filename_parents)
    
    # merge individual and parents 
    dfmerge=merge(dfind,dfparents,by=c("year","bsamplenum","pct") )
    dfmerge=dfmerge[dfmerge$pct!=100,]
    
    # fix the column names 
    ind30name=paste("pctind_",ii,"age30",sep="")
    ind45name=paste("pctind_",ii,"age45",sep="")
    
    
    
    dfmerge$pctindage30=dfmerge[[ind30name]]
    dfmerge$pctindage45=dfmerge[[ind45name]]
    
    
    dfmerge=dfmerge[,c("year","bsamplenum","pct","pctindage30","pctindage45","pctparentage30","pctparentage45")]
    return(dfmerge)
  })
  
  
  allboots=do.call(rbind,bootstrapout)
  
  
  
  
})

alllad=do.call(rbind,ladout)

write.csv(alllad, paste(filepathlad,"all_lad_bootstrap1_250.csv",sep=""))
